home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / LIB / TEXT_LIS.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  9.1 KB  |  321 lines

  1. package sub_arctic.lib;
  2.  
  3. import sub_arctic.output.style_manager;
  4. import sub_arctic.output.style;
  5. import sub_arctic.output.color_pair;
  6. import sub_arctic.output.drawable;
  7. import sub_arctic.constraints.std_function;
  8.  
  9. import java.awt.Color;
  10. import java.awt.Font;
  11. import java.awt.FontMetrics;
  12. import java.util.Vector;
  13.  
  14.  
  15. /**
  16.  * This class implements a list item which can display a text string. 
  17.  * It is basically a label which knows how to change its display based
  18.  * being selected.
  19.  * 
  20.  * @author Ian Smith
  21.  */
  22. public class text_list_element extends label implements list_element {
  23.  
  24.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  25.  
  26.   /**
  27.    * We store whether we are highlighted or not.  Initially, we aren't.
  28.    */
  29.   protected boolean _highlighted=false;
  30.  
  31.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  32.  
  33.   /**
  34.    * Create a text_list_element with a string. This will use the
  35.    * default font.
  36.    *
  37.    * @param String text the text display.
  38.    */
  39.   public text_list_element(String text) 
  40. {
  41.     this(text,null);
  42.   }
  43.  
  44.    //had:
  45.    //* @exception general PROPAGATED
  46.  
  47.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  48.  
  49.   /**
  50.    * Create a text_list_element with a string & a font. If you supply
  51.    * null for the font, you will get the system's default font.
  52.    * 
  53.    * @param String text the text display.
  54.    * @param Font   font the font to use (or null for the system default font).
  55.    */
  56.   public text_list_element(String text, Font font) 
  57. {
  58.  
  59.     super(text, font==null ? style_manager.default_font() : font);
  60.  
  61.     Color fg=style_manager.default_color_scheme().foreground();
  62.     Color bg=style_manager.default_color_scheme().text_background();
  63.     color_pair cp=new color_pair(fg,bg);
  64.  
  65.     /* set things up to look like the style expects */
  66.     set_draw_colors(cp);
  67.     set_boxed(false);
  68.     set_opaque(true);
  69.  
  70.     /* make sure we exactly enough room for a border */
  71.     set_h_spacing(2);
  72.     set_above_spacing(2);
  73.     set_below_spacing(manager.get_metrics(font()).getDescent()+2);
  74.  
  75.     /* now constrain our width to be the same as our parent */
  76.     set_w_constraint(std_function.offset(PARENT.W(),0));
  77.   }
  78.  
  79.    //had:
  80.    //* @exception general PROPAGATED
  81.  
  82.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  83.  
  84.   /**
  85.    * Implement the highlight behavior for this list element.
  86.    */
  87.   public void highlight() {
  88.     _highlighted=true;
  89.     damage_self();
  90.   }
  91.  
  92.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  93.  
  94.   /**
  95.    * Implement the unhighlight behavior for this list element.
  96.    */
  97.   public void unhighlight() {
  98.     _highlighted=false;
  99.     damage_self();
  100.   }
  101.  
  102.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  103.  
  104.   /**
  105.    * Return the convenient representation of this object. <P>
  106.    * 
  107.    * This is a convenient way to use subclassing with listboxes. If
  108.    * you return an object of your own from this method, you will
  109.    * receive it when you ask for listbox contents or selected
  110.    * sets. This lets you easily get at application semantics 
  111.    * in your code when using listboxes that display "strings". E.g.
  112.    * if you are displaying a list of filenames in the listbox,
  113.    * you might want to return File objects from this method so
  114.    * when the user selects a file, you won't have to go looking
  115.    * for that File object again.<P>
  116.    * 
  117.    * @return Object a string with the text of this object in it
  118.    */
  119.   public Object convert_to_object() {
  120.     return text();
  121.   }
  122.  
  123.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  124.  
  125.   /**
  126.    * Draw this object on the drawable provided.
  127.    * @param drawable d the drawable to draw on. 
  128.    */
  129.   public void draw_self_local(drawable d) {
  130.     Color fg, bg, splash;
  131.  
  132.     /* pick the colors on highlight status */
  133.     if (_highlighted) {
  134.       bg=draw_colors().foreground();
  135.       fg=draw_colors().background();
  136.     } else {
  137.       fg=draw_colors().foreground();
  138.       bg=draw_colors().background();
  139.     }
  140.  
  141.     /* clear the background if they asked for that */
  142.     if (opaque()) {
  143.       d.setColor(bg);
  144.  
  145.       /* if they are selected we need to do some work */
  146.       if (_highlighted) {
  147.     /* are they the focus? */
  148.     if (_focus) {
  149.       /* leave room for a one pixel splash border and a white pixel */
  150.       d.fillRect(2,2,w()-4,h()-4);
  151.     } else {
  152.       /* leave room for only one white pixel above */
  153.       d.fillRect(0,1,w()-1,h()-2);
  154.     }
  155.       } else {
  156.     /* simple case: not highlighted just clear the whole thing */
  157.     d.fillRect(0,0,w(),h());
  158.       }
  159.     }
  160.  
  161.     /* we only want the extra border if they are the focus */
  162.     if (_focus) {
  163.       /* they are highlighted, so put in the splash color border */
  164.       splash=style_manager.default_color_scheme().splash();
  165.       d.setColor(splash);
  166.       /* draw it */
  167.       d.drawRect(0,0,w()-1,h()-1);
  168.     }
  169.  
  170.     /* set font to our font, and start drawing in foreground */
  171.     d.setFont(font());
  172.     d.setColor(fg);
  173.  
  174.     /* put up the string */
  175.     d.drawString(text(), _h_spacing, _above_spacing+_metric.getAscent());
  176.   }
  177.  
  178.    //had:
  179.    //* @exception general PROPAGATED.
  180.  
  181.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  182.  
  183.   /**
  184.    * Return if the object is highlighted right now.
  185.    * @return boolean true if the object is currently highlighted
  186.    */
  187.   public boolean highlighted() {
  188.     return _highlighted;
  189.   }
  190.  
  191.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  192.  
  193.   /**
  194.    * This static function is a convenience for creating a Vector
  195.    * text_list_elements from an array of strings.
  196.    * @param String[] contents the strings to make elements out of.
  197.    * @param Font     font     the font to use (or pass null for a default font).
  198.    * @return Vector a vector suitable for use with set_contents() on 
  199.    *                listbox_display.
  200.    */
  201.   public static Vector make_contents_vector(String[] contents, Font font) {
  202.     Font f;
  203.     int i;
  204.     Vector retval=new Vector();
  205.  
  206.     /* make the items */
  207.     for (i=0; i<contents.length;++i) {
  208.       retval.addElement(new text_list_element(contents[i],font));
  209.     }
  210.  
  211.     /* return it */
  212.     return retval;
  213.   }
  214.  
  215.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  216.  
  217.   /**
  218.    * This static function is a convenience for creating a Vector
  219.    * text_list_elements from a Vector of strings.
  220.    * 
  221.    * @param Vector contents the strings to make elements out of.
  222.    * @param Font   font     the font to use (or pass null for a default font).
  223.    * @return Vector a vector suitable for use with set_contents() on 
  224.    *                listbox_display.
  225.    */
  226.   public static Vector make_contents_vector(Vector contents, Font font) {
  227.     Font f;
  228.     int i;
  229.     Vector retval=new Vector();
  230.     String txt;
  231.  
  232.     /* make the items */
  233.     for (i=0; i<contents.size();++i) {
  234.  
  235.       /* get the string out */
  236.       txt=(String)contents.elementAt(i);
  237.  
  238.       /* convert to an element */
  239.       retval.addElement(new text_list_element(txt,font));
  240.     }
  241.  
  242.     /* return it */
  243.     return retval;
  244.   }
  245.  
  246.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  247.  
  248.   /**
  249.    * Compute the preferred width for the parent.
  250.    * @return int the size of the text string being displayed + 4 pixels of 
  251.    *             border.
  252.    */
  253.   public int preferred_width() {
  254.     FontMetrics metrics=manager.get_metrics(font());
  255.     return (metrics.stringWidth(text())+4);
  256.   }
  257.  
  258.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  259.  
  260.   /**
  261.    * This variable holds our state of focusedness.
  262.    */
  263.   protected boolean _focus=false;
  264.  
  265.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  266.  
  267.   /**
  268.    * This method is used to inform the object that it has become the
  269.    * focus of a listbox. Listbox elements should draw themselves 
  270.    * differently to indicate this demarcation. Listbox elements may
  271.    * be the focus when they are not highlighted and vice-versa.
  272.    */
  273.   public void become_focus() {
  274.  
  275.     _focus=true;
  276.  
  277.     damage_self();
  278.   }
  279.  
  280.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  281.  
  282.   /**
  283.    * This method is used to inform the object that it is not
  284.    * the focus any longer.
  285.    */
  286.   public void lost_focus() {
  287.  
  288.     _focus=false;
  289.  
  290.     damage_self();
  291.   }
  292.  
  293.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  294.  
  295.   /**
  296.    * This method is used to query the object about whether or
  297.    * not it is the focus.
  298.    * @return boolean true if this object is the focus
  299.    */
  300.   public boolean focused() { return _focus;}
  301.  
  302.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  303. }
  304.  
  305. /*=========================== COPYRIGHT NOTICE ===========================
  306.  
  307. This file is part of the subArctic user interface toolkit.
  308.  
  309. Copyright (c) 1996 Scott Hudson and Ian Smith
  310. All rights reserved.
  311.  
  312. The subArctic system is freely available for most uses under the terms
  313. and conditions described in 
  314.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  315. and appearing in full in the lib/interactor.java source file.
  316.  
  317. The current release and additional information about this software can be 
  318. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  319.  
  320. ========================================================================*/
  321.